package xyz.cerberu.cet.resource.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.resource.dao.ResourceDao;
import xyz.cerberu.cet.resource.domian.Resource;
import xyz.cerberu.cet.subtitle.dao.SubtitleDao;
import xyz.cerberu.cet.subtitle.domain.Subtitle;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

/**
 * @author β世界
 * Created on 22:18
 * @Description
 */
@Service
public class ResourceServiceImpl {
    private final ResourceDao resourceDao;
    private final SubtitleDao subtitleDao;
    private final Global global;

    @Autowired
    public ResourceServiceImpl(ResourceDao resourceDao, SubtitleDao subtitleDao, Global global) {
        this.resourceDao = resourceDao;
        this.subtitleDao = subtitleDao;
        this.global = global;
    }

    /**
     * @author β世界 on 15:35 2020/7/14
     * 获取一定时间内的资源信息
     * @param localDate: 开始的时间
     * 在查询到的结果大于0的时候就缓存,存储的key就通过传递的参数来设置
     * @return java.util.List<com.Cet.resource.domian.Resource>
     **/
    @Cacheable(value = "resource",key = "#root.args",unless = "#result.size() == 0")
    public List<Resource> assignTimeResource(LocalDate localDate) {
        // 获取开始的时间
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        Date startTime = new Date(zonedDateTime.minusDays(2).toInstant().toEpochMilli());
        Date endTime = new Date(zonedDateTime.plusDays(1).toInstant().toEpochMilli());
        // 获取查询参数
        return resourceDao.selectIntervalTime(endTime,startTime);
    }

    /**
     * @author β世界 on 15:40 2020/7/14
     * 获取指定时间内的资源更新
     * @param localDate: 指定的时间
     * 在查询到的结果大于0的时候就缓存,存储的key就通过传递的参数来设置
     * @return java.util.List<com.Cet.resource.domian.Resource>
     **/
    @Cacheable(value = "resource",key = "#root.args",unless = "#result.size() == 0")
    public List<Resource> appointTime(LocalDate localDate){
        return resourceDao.selectByTime(Date.valueOf(localDate));
    }

    /**
     * @author β世界 on 20:52 2020/4/21
     * @Description 获取一部分的动漫信息
     * 在查询到的结果大于0的时候就缓存,存储的key就通过传递的参数来设置
     * @return java.util.List<com.Cet.resource.domian.Resource>
     **/
    @Cacheable(value = "resource",key = "#root.args",unless = "#result.size() == 0")
    public MultiValueMap<Subtitle,Resource> getAnimeFullResource(String aniId){
        // 用于存储不同的字幕组对应的资源
        MultiValueMap<Subtitle,Resource> map = new LinkedMultiValueMap<>();
        int[] subs = resourceDao.selectAnimeSubtitlegroups(aniId);
        if(subs == null){
            return map;
        }
        for(int subId : subs){
            Subtitle subtitle = subtitleDao.selectBySub_id(subId);
            // 根据sub_id和ani_id来获取动漫
            List<Resource> resources = resourceDao.selectByAniSub(aniId,subId);
            for(Resource resource : resources){
                // 将结果添加到map中
                map.add(subtitle,resource);
            }
        }
        return map;
    }


}
